package cn.t.repository.impl.device;

import cn.t.converter.device.DeviceHeartbeatConverter;
import cn.t.core.mode.YesNoEnum;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.core.enums.base.BaseStatusEnum;
import cn.t.model.device.DeviceHeartbeatDO;
import cn.t.persistence.entity.device.DeviceHeartbeatPO;
import cn.t.persistence.mapper.device.DeviceHeartbeatMapper;
import cn.t.repository.device.DeviceHeartbeatRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author l
 */
@Slf4j
@Repository
@AllArgsConstructor
public class DeviceHeartbeatRepositoryImpl extends RepositoryImpl<DeviceHeartbeatMapper, DeviceHeartbeatPO, DeviceHeartbeatConverter, DeviceHeartbeatDO> implements DeviceHeartbeatRepository {


    @Override
    public List<DeviceHeartbeatDO> findByModel(DeviceHeartbeatDO deviceHeartbeatDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ? " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<DeviceHeartbeatPO> list = getWrapper(deviceHeartbeatDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<DeviceHeartbeatPO> getWrapper(DeviceHeartbeatDO deviceHeartbeatDO) {
        LambdaQueryChainWrapper<DeviceHeartbeatPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(deviceHeartbeatDO.getDeviceId())) {
            wrapper.eq(DeviceHeartbeatPO::getDeviceId, deviceHeartbeatDO.getDeviceId());
        }

        if (!ObjectUtils.isEmpty(deviceHeartbeatDO.getType())) {
            wrapper.eq(DeviceHeartbeatPO::getType, deviceHeartbeatDO.getType());
        }

        if (!ObjectUtils.isEmpty(deviceHeartbeatDO.getPin())) {
            wrapper.eq(DeviceHeartbeatPO::getPin, deviceHeartbeatDO.getPin());
        }
        if (!ObjectUtils.isEmpty(deviceHeartbeatDO.getImei())) {
            wrapper.eq(DeviceHeartbeatPO::getImei, deviceHeartbeatDO.getImei());
        }
        if (!ObjectUtils.isEmpty(deviceHeartbeatDO.getToken())) {
            wrapper.eq(DeviceHeartbeatPO::getToken, deviceHeartbeatDO.getToken());
        }
        if (!ObjectUtils.isEmpty(deviceHeartbeatDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(deviceHeartbeatDO.getGmtCreatedEnd())) {
            wrapper.between(DeviceHeartbeatPO::getGmtCreated, deviceHeartbeatDO.getGmtCreatedStart(), deviceHeartbeatDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(deviceHeartbeatDO.getGmtModifiedStart()) && !ObjectUtils.isEmpty(deviceHeartbeatDO.getGmtModifiedEnd())) {
            wrapper.between(DeviceHeartbeatPO::getGmtModified, deviceHeartbeatDO.getGmtModifiedStart(), deviceHeartbeatDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(deviceHeartbeatDO.getIsDeleted())) {
            wrapper.eq(DeviceHeartbeatPO::getIsDeleted, deviceHeartbeatDO.getIsDeleted());
        } else {
            wrapper.eq(DeviceHeartbeatPO::getIsDeleted, YesNoEnum.FALSE.getCode());
        }

        if (!ObjectUtils.isEmpty(deviceHeartbeatDO.getStatus())) {
            wrapper.eq(DeviceHeartbeatPO::getStatus, deviceHeartbeatDO.getStatus());
        } else {
            wrapper.eq(DeviceHeartbeatPO::getStatus, String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
        }
        return wrapper;
    }

    @Override
    public long count(DeviceHeartbeatDO deviceHeartbeatDO) {
        return getWrapper(deviceHeartbeatDO).count();
    }


}
